package com.hjw.controller.adm;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.util.SaResult;
import com.hjw.entity.Type;
import com.hjw.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Administrator
 * @创建时间：2025-04-23
 * @文件名称：satoken_sobuk - com.hjw.controller.adm
 */
@Slf4j
@SaCheckRole("adm")
@RestController
@RequestMapping("/api/adm/type")
public class AdmTypeController {

    @Autowired
    private TypeService typeService;

    @PostMapping("/addOrUpdate") // /api/adm/type/addOrUpdate
    public SaResult addOrUpdate(@RequestBody Type type){

        log.info("type:{}", type);

        if (type == null){
            return SaResult.get(-200, "数据异常，添加失败", null);
        }

        // 判断是否为添加操作
        if (type.getId() == 0){
            int add = typeService.add(type);
            if(add != 0){
                return SaResult.get(200, "添加成功", null);
            }
            return SaResult.get(203, "数据异常，添加失败", null);
        }

        Map<String, Object> map = new HashMap<>(5);
        map.put("id", type.getId());
        map.put("name", type.getName());
        map.put("pid", type.getPid());
        map.put("prio", type.getPrio());
        map.put("isdel", type.getIsdel());

        // 为修改操作
        int updated = typeService.update(map);

        if (updated != 0){
            return SaResult.get(200, "更新成功", null);
        }
        return SaResult.get(-200, "数据异常，更新失败", null);
    }

    @GetMapping("/remove/{id}") // /api/adm/type/remove/{id}
    public SaResult remove(@PathVariable("id") Integer tid) {

        int delete = typeService.delete(tid);
        if (delete == 0){
            return SaResult.get(-200, "数据异常，删除失败", null);
        }

        Map<String, Object> result = this.getAll();
        return SaResult.get(200, "删除成功", result);
    }

    @GetMapping("/del/{id}") // /api/adm/type/del/{id}
    public SaResult del(@PathVariable("id") Integer tid) {
        if (tid == 0) {
            return SaResult.get(-200, "数据异常，删除失败", null);
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("id", tid);
        // 做假删除标识
        map.put("isdel", 1);
        int delete = typeService.update(map);
        if (delete == 0){
            return SaResult.get(-200, "数据异常，删除失败", null);
        }
        Map<String, Object> result = this.getAll();
        return SaResult.get(200, "删除成功", result);
    }

    @GetMapping("/findAll2") // /api/adm/type/findAll2
    public SaResult findAll() {
        Map<String, Object> result = this.getAll();
        return SaResult.get(200, "获取成功", result);
    }

    @GetMapping("/findById/{id}") // /api/adm/type/findById/{id}
    public SaResult findById(@PathVariable("id") Integer id) {
        log.info("id:{}", id);
        if (id == 0) {
            return SaResult.get(-200, "数据异常，获取失败", null);
        }
        Type type = typeService.findById(id);
        return SaResult.get(200, "获取成功", type);
    }


    public Map<String, Object> getAll(){
        List<Type> all = typeService.findAll();
        //一级类型
        List<Type> category = new ArrayList<>(10);
        // 二级类型
        List<Type> types = new ArrayList<>(10);
        // 一级类型id
        Set<Integer> id = new HashSet<>(10);

        for (Type type : all) {
            if (type.getPid() == 0) {
                category.add(type);
                id.add(type.getId());
                continue;
            }
            types.add(type);
        }

        //得到所有二级类型的返回数组
        List<Object> list = new ArrayList<>();

        for (Integer i : id) {
            List<Object> t = new ArrayList<>();
            t.add(i);
            for (Type type : types) {
                if (type.getPid() == i) {
                    t.add(type);
                }
            }
            if (t.size() == 1) {
                continue;
            }
            list.add(t);
        }
        Map<String, Object > map = new HashMap<>(2);
        map.put("category", category);
        map.put("types", list);
        return map;
    }

}
